# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: linchuan

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo


def generate():
  mea = 'Spot.Bitstamp.v2'
  me = 'Spot.Bitstamp'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://www.bitstamp.net/api/v2/trading-pairs-info/').json()

  pis = []
  for info in data:

    native_symbol = info['name']
    str_arr = native_symbol.split("/")
    native_base = str_arr[0].lower()
    native_quote = str_arr[1].lower()
    base = symbol_info.native_to_norm_currency(mea, native_base)
    quote = symbol_info.native_to_norm_currency(mea, native_quote)
    symbol = "%s-%s" % (base, quote)
    native_symbol = "%s%s" % (native_base, native_quote)
    min_amount = float(info['minimum_order'].split(' ')[0])

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=10 ** (-info['counter_decimals']),
        qty_ticksize=10 ** (-info['base_decimals']),
        min_amount=min_amount,
        is_inverse=False,
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = [info['native_quote'] for info in data]
  base_currency_list = [info['native_base'] for info in data]
  currency_list = base_currency_list + quote_currency_list
  res = {
      'symbol_list': list(set(symbol_list)),
      'quote_currency_list': list(set(quote_currency_list)),
      'native_currency_list': list(set(currency_list)),
  }
  return res


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
